package com.xhtt.modules.task.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhtt.modules.sys.entity.SysDeptEntity;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.service.SysDeptService;
import com.xhtt.modules.sys.service.SysUserService;
import com.xhtt.modules.task.entity.SuperTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.FutureTask;

@Component
public class TaskDetailSchedule<T extends SuperTask> {
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysDeptService sysDeptService;

    public void getTaskDetail(T task) {
        //找确认人1名字
        FutureTask<String> confirm1Name = new FutureTask<>(() -> {
            if (null != task.getConfirm1User()) {
                SysUserEntity u = sysUserService.getOne(new LambdaQueryWrapper<SysUserEntity>().eq(SysUserEntity::getUserId, task.getConfirm1User()).select(SysUserEntity::getUserId, SysUserEntity::getName));
                return null == u ? "" : u.getName();
            }
            return "";
        });
        taskExecutor.submit(confirm1Name);
        //找确认人2名
        FutureTask<String> confirm2Name = new FutureTask<>(() -> {
            if (null != task.getConfirm2User()) {
                SysUserEntity u = sysUserService.getOne(new LambdaQueryWrapper<SysUserEntity>().eq(SysUserEntity::getUserId, task.getConfirm2User()).select(SysUserEntity::getUserId, SysUserEntity::getName));
                return null == u ? "" : u.getName();
            }
            return "";
        });
        taskExecutor.submit(confirm2Name);
        //找组名
        FutureTask<String> team = new FutureTask<>(() -> {
            if (null != task.getExecTeamId()) {
                SysDeptEntity dept = sysDeptService.getOne(new LambdaQueryWrapper<SysDeptEntity>().eq(SysDeptEntity::getDeptId, task.getExecTeamId()));
                return null == dept ? "" : dept.getName();
            }
            return "";
        });
        taskExecutor.submit(team);
        //找操作人名
        FutureTask<String> operUser = new FutureTask<>(() -> {
            if (null != task.getUserMainId()) {
                SysUserEntity u = sysUserService.getOne(new LambdaQueryWrapper<SysUserEntity>().eq(SysUserEntity::getUserId, task.getUserMainId()).select(SysUserEntity::getUserId, SysUserEntity::getName));
                return null == u ? "" : u.getName();
            }
            return "";
        });
        taskExecutor.submit(operUser);


        try {
            task.setConfirm1UserName(confirm1Name.get());
            task.setConfirm2UserName(confirm2Name.get());
            task.setExecTeamName(team.get());
            task.setUserMainName(operUser.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
